package com.book.dacnhantam;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.res.Resources;
import android.graphics.Picture;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebView.PictureListener;
import android.widget.ProgressBar;
import android.widget.Toast;
import com.book.dacnhantam.data.Chapter;
import com.book.dacnhantam.data.ChapterProvider;
import com.google.ads.AdRequest;
import com.google.ads.AdView;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class ChapterActivity extends Activity
{
  public static final String BOOKMARK_SCROLL_CHAPTER_PREFS_KEY = "BOOKMARK_SCROLL_CHAPTER_PREFS_KEY";
  public static final String BOOKMARK_SCROLL_PREFS_KEY = "BOOKMARK_SCROLL_PREFS_KEY";
  public static final String SCROLL_CHAPTER_PREFS_KEY = "SCROLL_CHAPTER_PREFS_KEY";
  public static final String SCROLL_PREFS_KEY = "SCROLL_PREFS_KEY";
  protected static final String TAG = "ChapterView";
  AdView adView;
  private SharedPreferences chapterPreferences;
  private Chapter currentChapter;
  private SharedPreferences.Editor editor;
  boolean isFistTime;
  private Chapter lastChapter;
  long lastTouch;
  ProgressBar loadingProgressBar;
  ProgressBar loadingTitle;
  private ChapterProvider mChapterProvider;
  private Chapter nextChapter;
  private int scrollToPos;
  Activity thisActivity;
  private String title;
  WebView webview;

  private void openOtherChapter(Chapter paramChapter)
  {
    Intent localIntent = new Intent(this, ChapterActivity.class);
    if (paramChapter == null);
    while (true)
    {
      return;
      localIntent.putExtra("chapter_id", paramChapter.getId());
      localIntent.putExtra("scroll_to", 0);
      startActivity(localIntent);
      finish();
    }
  }

  private String readTextFromResource(int paramInt)
  {
    InputStream localInputStream = getResources().openRawResource(paramInt);
    ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();
    try
    {
      int j;
      for (int i = localInputStream.read(); ; i = j)
      {
        if (i == -1)
        {
          localInputStream.close();
          return localByteArrayOutputStream.toString();
        }
        localByteArrayOutputStream.write(i);
        j = localInputStream.read();
      }
    }
    catch (IOException localIOException)
    {
      while (true)
        localIOException.printStackTrace();
    }
  }

  public void onCreate(Bundle paramBundle)
  {
    super.onCreate(paramBundle);
    getWindow().setFlags(1024, 1024);
    setRequestedOrientation(1);
    setContentView(2130903042);
    this.thisActivity = this;
    this.isFistTime = true;
    getWindow().setFeatureInt(2, -1);
    this.webview = ((WebView)findViewById(2131230727));
    this.adView = ((AdView)findViewById(2131230728));
    AdRequest localAdRequest = new AdRequest();
    this.adView.loadAd(localAdRequest);
    this.loadingProgressBar = ((ProgressBar)findViewById(2131230726));
    this.mChapterProvider = ChapterProvider.getChapterProvider();
    this.webview.setScrollBarStyle(0);
    this.chapterPreferences = getSharedPreferences("CHAPTER", 0);
    this.editor = this.chapterPreferences.edit();
    Intent localIntent = getIntent();
    int i = localIntent.getIntExtra("chapter_id", -1);
    this.scrollToPos = localIntent.getIntExtra("scroll_to", 0);
    if (i == -1)
      finish();
    this.currentChapter = this.mChapterProvider.getChapter(i);
    this.nextChapter = this.mChapterProvider.getNextChapter(i);
    this.lastChapter = this.mChapterProvider.getLastChapter(i);
    this.title = this.currentChapter.getWindowTitle();
    this.webview.loadDataWithBaseURL("", readTextFromResource(this.currentChapter.getResource()), "text/html", "utf-8", null);
    this.webview.setWebChromeClient(new WebChromeClient()
    {
      public void onProgressChanged(WebView paramWebView, int paramInt)
      {
        super.onProgressChanged(paramWebView, paramInt);
        ChapterActivity.this.loadingProgressBar.setProgress(paramInt);
        ChapterActivity.this.thisActivity.setProgress(paramInt);
        ChapterActivity.this.thisActivity.setTitle("Loading");
        if (paramInt == 100)
        {
          ChapterActivity.this.loadingProgressBar.setVisibility(8);
          ChapterActivity.this.thisActivity.setTitle(ChapterActivity.this.title);
          Toast.makeText(ChapterActivity.this.getApplicationContext(), ChapterActivity.this.getString(2131099652), 0).show();
        }
        while (true)
        {
          return;
          ChapterActivity.this.loadingProgressBar.setVisibility(0);
        }
      }
    });
    this.webview.setOnTouchListener(new View.OnTouchListener()
    {
      public boolean onTouch(View paramView, MotionEvent paramMotionEvent)
      {
        if (paramMotionEvent.getAction() == 0)
        {
          long l = System.currentTimeMillis();
          if (l - ChapterActivity.this.lastTouch < 500L)
            ChapterActivity.this.webview.scrollBy(0, ChapterActivity.this.webview.getHeight() - ChapterActivity.this.adView.getHeight());
          ChapterActivity.this.lastTouch = l;
        }
        return false;
      }
    });
    this.webview.setPictureListener(new WebView.PictureListener()
    {
      public void onNewPicture(WebView paramWebView, Picture paramPicture)
      {
        if (ChapterActivity.this.isFistTime)
        {
          Log.e("ChapterView", "LOAD: " + ChapterActivity.this.scrollToPos);
          ChapterActivity.this.webview.scrollTo(0, ChapterActivity.this.scrollToPos);
          ChapterActivity.this.isFistTime = false;
        }
      }
    });
  }

  public boolean onCreateOptionsMenu(Menu paramMenu)
  {
    getMenuInflater().inflate(2131165184, paramMenu);
    return super.onCreateOptionsMenu(paramMenu);
  }

  public boolean onOptionsItemSelected(MenuItem paramMenuItem)
  {
    switch (paramMenuItem.getItemId())
    {
    default:
    case 2131230735:
    case 2131230737:
    case 2131230736:
    }
    while (true)
    {
      return super.onOptionsItemSelected(paramMenuItem);
      openOtherChapter(this.lastChapter);
      continue;
      openOtherChapter(this.nextChapter);
      continue;
      int i = this.webview.getScrollY();
      this.editor.putInt("BOOKMARK_SCROLL_PREFS_KEY", i);
      this.editor.putInt("BOOKMARK_SCROLL_CHAPTER_PREFS_KEY", this.currentChapter.getId());
      if (this.editor.commit())
      {
        Toast.makeText(this, getString(2131099653), 0).show();
        continue;
      }
      Toast.makeText(this, getString(2131099654), 0).show();
    }
  }

  public boolean onPrepareOptionsMenu(Menu paramMenu)
  {
    if (this.lastChapter == null)
      paramMenu.getItem(0).setEnabled(false);
    if (this.nextChapter == null)
      paramMenu.getItem(2).setEnabled(false);
    return super.onPrepareOptionsMenu(paramMenu);
  }

  protected void onResume()
  {
    super.onResume();
  }

  protected void onStop()
  {
    super.onStop();
    int i = this.webview.getScrollY();
    this.editor.putInt("SCROLL_PREFS_KEY", i);
    this.editor.putInt("SCROLL_CHAPTER_PREFS_KEY", this.currentChapter.getId());
    boolean bool = this.editor.commit();
    Log.e("ChapterView", "PUT: " + bool + ":" + i);
  }
}

/* Location:           C:\Users\chutieucodon\Desktop\dec\com.book.dacnhantam-36fb9c182ef79376b618c867d3d1c347\classes_dex2jar.jar
 * Qualified Name:     com.book.dacnhantam.ChapterActivity
 * JD-Core Version:    0.6.0
 */